package com.xforceplus.bsstool.log;

import com.xforceplus.bsstool.config.ConfigMain;
import com.xforceplus.bsstool.db.NewBssDB;
import com.xforceplus.bsstool.db.NewUcenterDB;
import com.xforceplus.bsstool.db.OldBssDB;
import com.xforceplus.bsstool.db.OldUcenterDB;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LogMain {
  /** INFO 输出*/
  private  File infoFile = null;
  private  FileWriter infoFw = null;
  private  int infoCount = 0;
  /** WARN 输出*/
  private  File warnFile = null;
  private  FileWriter warnFw = null;
  private  int warnCount = 0;
  /** ERROR 输出*/
  private  File errorFile = null;
  private  FileWriter errorFw = null;
  private  int errorCount = 0;

  public int getInfoCount() {
    return infoCount;
  }

  public int getWarnCount() {
    return warnCount;
  }

  public int getErrorCount() {
    return errorCount;
  }

  public  void startLog(String subDirectory) throws IOException {
    //INFO 初始化
    if (infoFw != null){
      infoFw.flush();
      infoFw.close();
    }
    if (infoFile != null) {
      infoFile.delete();
    }
    infoFile = new File(ConfigMain.LOG_PATH+subDirectory+"\\info.txt");
    infoFw = new FileWriter(infoFile);

    //WARN 初始化
    if (warnFw != null){
      warnFw.flush();
      warnFw.close();
    }
    if (warnFile != null) {
      warnFile.delete();
    }
    warnFile = new File(ConfigMain.LOG_PATH+subDirectory+"\\warn.txt");
    warnFw = new FileWriter(warnFile);

    //ERROR 初始化
    if (errorFw != null){
      errorFw.flush();
      errorFw.close();
    }
    if (errorFile != null) {
      errorFile.delete();
    }
    errorFile = new File(ConfigMain.LOG_PATH+subDirectory+"\\error.txt");
    errorFw = new FileWriter(errorFile);

    System.out.println("日志记录开始..........");
  }

  public  void endLog() throws IOException {
    if (infoFw != null){
      infoFw.flush();
      infoFw.close();
    }
    if (warnFw != null){
      warnFw.flush();
      warnFw.close();
    }
    if (errorFw != null){
      errorFw.flush();
      errorFw.close();
    }
    System.out.println("日志记录完毕！");
    //释放数据库连接
    NewBssDB.releaseConnection();
    OldBssDB.releaseConnection();
    NewUcenterDB.releaseConnection();
    OldUcenterDB.releaseConnection();
  }

  public  void info(String infoMsg) throws IOException {
    infoFw.append(infoMsg).append("\n");
    infoCount++;
  }

  public  void warn(String warnMsg) throws IOException {
    warnFw.append(warnMsg).append("\n");
    warnCount++;
  }

  public  void error(String errorMsg) throws IOException {
    errorFw.append(errorMsg).append("\n");
    errorCount++;
  }


  public static void main(String[] args) throws IOException {
    LogMain logger = new LogMain();

    logger.startLog("base");
    logger.info("zsdfsfsdfdsff");
    logger.info("3342342423423");
    logger.info("sadfsdfsddfdsfsdfs");
    logger.warn("sdfskjlkwarn");
    logger.warn("sdfskjlkwarn");
    logger.warn("sdfskjlkwarn");

    logger.endLog();

  }
}
